package org.lilred.nzbberry.views;

import org.lilred.nzbberry.config.Resizer;
import org.lilred.nzbberry.controllers.QueueController;
import org.lilred.nzbberry.models.QueueNzbModel;
import org.lilred.nzbberry.util.FontFactory;
import org.lilred.nzbberry.util.ScaleImage;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Graphics;

public class QueueSlotView extends SlotView {
	private QueueNzbModel model;
	public static Bitmap progressIncompleteBitmap = null;
	public static Bitmap progressCompleteBitmap = null;
	public static Bitmap progressPausedBitmap = null;
	public static Bitmap queueDownloadingBitmap = null;
	public static Bitmap queuePausedBitmap = null;
	public static Bitmap queueQueuedBitmap = null;
	
	public QueueSlotView(QueueNzbModel model) {
		super();
		this.model = model;
	}
	
	protected void layout(int width, int height) {
		super.layout(width, height);
		initalizeBitmaps(width);
	}
	
	public void initalizeBitmaps(int width) {
		if (progressIncompleteBitmap == null) {
			EncodedImage e = EncodedImage.getEncodedImageResource("progress-incomplete.png");
			e = ScaleImage.toWidth(e, width);
			progressIncompleteBitmap = e.getBitmap();
		}
		if (progressCompleteBitmap == null) {
			EncodedImage e = EncodedImage.getEncodedImageResource("progress-complete.png");
			e = ScaleImage.toWidth(e, width);
			progressCompleteBitmap = e.getBitmap();
		}
		if (progressPausedBitmap == null) {
			EncodedImage e = EncodedImage.getEncodedImageResource("progress-paused.png");
			e = ScaleImage.toWidth(e, width);
			progressPausedBitmap = e.getBitmap();
		}
		if (queueDownloadingBitmap == null) {
			EncodedImage e = EncodedImage.getEncodedImageResource("QueueDownloading.png");
			e = ScaleImage.toWidth(e, Resizer.resize(e.getWidth()));
			queueDownloadingBitmap = e.getBitmap();
		}
		if (queuePausedBitmap == null) {
			EncodedImage e = EncodedImage.getEncodedImageResource("QueuePaused.png");
			e = ScaleImage.toWidth(e, Resizer.resize(e.getWidth()));
			queuePausedBitmap = e.getBitmap();
		}
		if (queueQueuedBitmap == null) {
			EncodedImage e = EncodedImage.getEncodedImageResource("QueueQueued.png");
			e = ScaleImage.toWidth(e, Resizer.resize(e.getWidth()));
			queueQueuedBitmap = e.getBitmap();
		}
	}
	
	public void setModel(QueueNzbModel model) {
		this.model = model;
		invalidate();
	}

	protected void paint(Graphics g) {
		if (isFocus()) {
			g.setColor(0xf9f9f9);
		} else if (model.getIndex() % 2 == 1) {
			g.setColor(0xe0e0e0); 
		} else {
			g.setColor(0xd0d0d0);
		}
		g.setGlobalAlpha(220);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.setGlobalAlpha(255);
		
		//Draw the white line on the top
		g.setColor(0xFFFFFF);
		g.drawLine(0, 0, getWidth(), 0);
		
		//Draw the dark grey line on the bottom
		g.setColor(0xb1b1b1);
		g.drawLine(0, getHeight() - 1, getWidth(), getHeight() - 1);

		g.setGlobalAlpha(255);
		//Draw the filename
		g.setFont(FontFactory.getFont(Resizer.resize(20)));
		g.setColor(0x000000);
		g.drawText(model.getFilename(), queueDownloadingBitmap.getWidth() + 2 * Resizer.resize(6), Resizer.resize(9));
		
		//Draw the incomplete portion of progress bar
		g.setGlobalAlpha(200);
		g.drawBitmap(
				queueDownloadingBitmap.getWidth() + 2 * Resizer.resize(6), 
				Resizer.resize(33), 
				progressIncompleteBitmap.getWidth(), 
				progressIncompleteBitmap.getHeight(), 
				progressIncompleteBitmap, 0, 0);
		//Draw the completed portion of the progress bar

		Bitmap bitmapToDraw = null;
		if (model.isPaused()) bitmapToDraw = queuePausedBitmap;
		else if (model.getStatus().compareTo("Queued") == 0) bitmapToDraw = queueQueuedBitmap;
		else bitmapToDraw = queueDownloadingBitmap;

		g.drawBitmap(
				queueDownloadingBitmap.getWidth() + 2 * Resizer.resize(6),
				Resizer.resize(33),
				(int) (progressCompleteBitmap.getWidth() * model.getPercentage()),
				progressCompleteBitmap.getHeight(),
				(bitmapToDraw == queueDownloadingBitmap) ? progressCompleteBitmap : progressPausedBitmap, //Paused bitmap is greyscale 
				0, 0);
		g.setGlobalAlpha(255);
		
		g.drawBitmap(Resizer.resize(6), getHeight() / 2 - bitmapToDraw.getHeight() / 2, bitmapToDraw.getWidth(), bitmapToDraw.getHeight(), bitmapToDraw, 0, 0);
		
		//Draw the focus border
		g.setColor(0xFFFF00);
		if (isFocus()) {
			g.drawRect(0, 0, getWidth(), getHeight());
		}
		g.setColor(0x000000);
		
		g.setFont(FontFactory.getFont(Resizer.resize(15)));
		g.drawText(model.getSize(), queueDownloadingBitmap.getWidth() + 2 * Resizer.resize(6) + Resizer.resize(5), Resizer.resize(40));
		String mbLeft = model.getMbLeft() + " MB left";
		int advance = g.getFont().getAdvance(mbLeft);
		g.drawText(mbLeft, getWidth() - advance - Resizer.resize(9), Resizer.resize(40));
	}

	protected void onFocus(int direction) {
		super.onFocus(direction);
		AppView.getInstance().appManager.updateFooter("Position: " + (model.getIndex() + 1) + ", Remaining: " + model.getTimeleft());
	}
}
